//
//  BSCollectionViewLayout.m
//  PhotographyStudi
//
//  Created by 叶一枫 on 2017/5/23.
//  Copyright © 2017年 叶一枫. All rights reserved.
//

#import "BSCollectionViewLayout.h"

@implementation BSCollectionViewLayout

-(instancetype)initWithItemsHeightBlock:(HeightBlock)block marginX:(CGFloat)marginX colCount:(CGFloat)count{
    self = [super init];
    if (self) {
        self.heightBlock = block;
        _colCount = count;
        _colMargin = marginX;
    }
    return self;
}

-(NSMutableArray *)colsHeight{
    if(!_colsHeight){
        NSMutableArray * array = [NSMutableArray array];
        for(int i =0;i<_colCount;i++){
            //这里可以设置初始高度
            [array addObject:@(0)];
        }
        _colsHeight = [array mutableCopy];
    }
    return _colsHeight;
}
//需要复写5个方法

//布局前的的初始工作
- (void)prepareLayout{
    
    [super prepareLayout];
    self.colWidth = (self.collectionView.frame.size.width -(self.colCount+1)*self.colMargin)/self.colCount;
    //重新加载
    self.colsHeight = nil;
    
    self.mutAttribute = [NSMutableArray array];
    
    /* 自定义collectionView的头
     UICollectionViewLayoutAttributes *header = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathWithIndex:0]];
     header.frame = CGRectMake(0, -120, VIEW_W, 120);
     [self.mutAttribute addObject:header];
     */
    
    NSInteger items = [self.collectionView numberOfItemsInSection:0];
    for (int i=0; i<items; i++) {
        UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [self.mutAttribute addObject:att];
    }
    
    /* 自定义collectionView的尾
     UICollectionViewLayoutAttributes *footer = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathWithIndex:0]];
     NSNumber *longest = self.colsHeight[0];
     
     for (int i=0; i<self.colsHeight.count; i++) {
     NSNumber *rolHeight = self.colsHeight[i];
     if (longest.floatValue<rolHeight.floatValue) {
     longest = rolHeight;
     }
     }
     footer.frame = CGRectMake(0, longest.floatValue>=(VIEW_H - 104)?longest.floatValue:(VIEW_H - 104), VIEW_W, 120);
     
     [self.mutAttribute addObject:footer];
     */
}

//内容尺寸
-(CGSize)collectionViewContentSize{
    
    NSNumber *longest = self.colsHeight[0];
    
    for (int i=0; i<self.colsHeight.count; i++) {
        NSNumber *rolHeight = self.colsHeight[i];
        if (longest.floatValue<rolHeight.floatValue) {
            longest = rolHeight;
        }
    }
    
    return CGSizeMake(self.collectionView.frame.size.width, longest.floatValue);
}


//为每一个item设置属性

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    NSNumber *shortest = self.colsHeight[0];
    NSInteger shortCol = 0;
    for (int i=0; i<self.colsHeight.count; i++) {
        NSNumber *rolHeight = self.colsHeight[i];
        if (shortest.floatValue>rolHeight.floatValue) {
            shortest = rolHeight;
            shortCol = i;
        }
    }
    
    CGFloat x = (shortCol+1)*self.colMargin + shortCol*self.colWidth;
    CGFloat y = shortest.floatValue + self.colMargin;
    
    //获取cell的高度
    CGFloat height = 0;
    
    if (self.heightBlock) {
        height = self.heightBlock(indexPath);
        
    }
    attributes.frame = CGRectMake(x, y, self.colWidth, height);
    self.colsHeight[shortCol] = @(shortest.floatValue + self.colMargin+height);
    return attributes;
    
}

//获取item的属性
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    return [self.mutAttribute filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        
        return CGRectIntersectsRect(rect, [evaluatedObject frame]);
    }]];
}

//这个方法是会在cell时重新布局并调用repareLayout方法
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


@end
